package server;

import java.util.ArrayList;

import constants.Cmd;
import constants.Rights;

public class RecordCollection {
	private ArrayList<Record> records;
	private ControlList cl;
	public RecordCollection(ControlList controlList){
		this.cl=controlList;
		records=new ArrayList<Record>();
		addRecord(Cmd.newRecordString("kalle", "kajsa", "lth", "Har varit sjuk"),"doc1");
		addRecord(Cmd.newRecordString("patient1", "nurse1", "lth", "Har varit sjuk"),"doc1");
	}
	public boolean addRecord(String fromClient, String subjectName) {
		//#NEW#Kalle#Kajsa#Lth#Har varit sjujk
		if(cl.getAccessRights(subjectName).equals(Rights.doctorAccess)){
			System.out.println(fromClient+"#"+subjectName);
			Record r=new Record(fromClient+"#"+subjectName);
			records.add(r);
			System.out.println(r.toString());
			return true;
		}else{
			System.out.println("Server-> "+subjectName+" failed to create record due to accessrights");
			return false;
		}	
	}
	public boolean delRecord(String substring, String subjectName){
		for(Record r: records){
			if(r.getPatient().equals(substring) && subjectName.equals("agency1")){
				records.remove(r);
				return true;
			}
		}
		return false;
	}
	
	public String getRecord(String patientName, String subjectName) {	
		for(Record r: records){
			if(r.getPatient().equals(patientName)){
				return r.getData(subjectName);
			}
		}
		return null;
	}
	public boolean editRecord(String substring, String subjectName) {
		String[] str = substring.split("#");
		String patient =str[0];
		String dataToAppend =str[1];
		for(Record r: records){
			if(r.getPatient().equals(patient)){
				return r.appendData(dataToAppend, subjectName);
			}
		}
		return false;
	}
	
}
